package com.pai4j.user.repository.dao;

import com.pai4j.user.repository.entity.GroupOperationLogEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IGroupOperationLogDAO extends JpaSpecificationExecutor<GroupOperationLogEntity>, JpaRepository<GroupOperationLogEntity, Long> {

    /**
     * 根据群ID查找操作日志（分页）
     */
    Page<GroupOperationLogEntity> findByGroupPaiIdOrderByCreateDateDesc(String groupPaiId, Pageable pageable);

    /**
     * 根据群ID和操作类型查找操作日志
     */
    List<GroupOperationLogEntity> findByGroupPaiIdAndOperationTypeOrderByCreateDateDesc(String groupPaiId, Integer operationType);

    /**
     * 根据操作人查找操作日志
     */
    Page<GroupOperationLogEntity> findByOperatorAccountOrderByCreateDateDesc(String operatorAccount, Pageable pageable);

    /**
     * 根据群ID和时间范围查找操作日志
     */
    @Query("SELECT gol FROM GroupOperationLogEntity gol WHERE gol.groupPaiId = :groupPaiId AND gol.createDate BETWEEN :startDate AND :endDate ORDER BY gol.createDate DESC")
    List<GroupOperationLogEntity> findByGroupPaiIdAndDateRange(@Param("groupPaiId") String groupPaiId, 
                                                               @Param("startDate") Date startDate, 
                                                               @Param("endDate") Date endDate);

    /**
     * 统计群操作次数
     */
    @Query("SELECT COUNT(gol) FROM GroupOperationLogEntity gol WHERE gol.groupPaiId = :groupPaiId AND gol.operationType = :operationType")
    Long countByGroupPaiIdAndOperationType(@Param("groupPaiId") String groupPaiId, @Param("operationType") Integer operationType);

    /**
     * 删除群操作日志（用于解散群聊）
     */
    void deleteByGroupPaiId(String groupPaiId);
}
